package com.netcracker.studportal.server.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.netcracker.studportal.server.dao.SubjectDAO;
import com.netcracker.studportal.server.model.Subject;
import com.netcracker.studportal.server.util.HibernateUtil;

public class SubjectDAOImpl implements SubjectDAO {
	private static final Logger LOG = Logger.getLogger(SubjectDAOImpl.class);

	@Override
	public void Add(Subject sb) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(sb);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public void Update(Subject newSubject) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(newSubject);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Subject getById(Long id) throws Exception {
		Session session = null;
		Subject sb = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			sb = (Subject) session.load(Subject.class, id);
			//TODO what is there initiaized?
			// does not work without this
			Hibernate.initialize(sb);
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();

			}
		}
		return sb;
	}

	@Override
	public void Delete(Subject subj) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(subj);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Collection<Subject> getAll() throws Exception {
		Session session = null;
		List<Subject> subject = new ArrayList<Subject>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			subject = session.createCriteria(Subject.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return subject;
	}

	/**
	 * Realization of singleton.
	 */
	private SubjectDAOImpl() {}
	private static SubjectDAO instance;
	public static SubjectDAO getInstance() {
		if (instance == null) {
			instance = new SubjectDAOImpl();
		}
		return instance;
	}
}
